package com.changli01._collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;

public class CollectionTest {

    @Test
    //Collection 接口中的ArrayList
//          add(Object o)
//        addAll (Collection other)
    public void test() {
        Collection c = new ArrayList();
        c.add(1);
        c.add("2");
        c.add(new Object());
        c.add(new Person("sail",19));
        System.out.println(c);

    }
    @Test
    public void test2() {
        Collection c = new ArrayList();
        c.add(1);
        c.add("2");
        Collection c2 = new ArrayList();
        c2.add(new Person("sail",19));
        c2.add(new Object());
        System.out.println(c.size()); // 2
        //add (Collection c) addAll (Collection c)
//        前者 将整个 Collection 放入 列表中
//        而后者 将Collection 解构 后放入List
//        c.add(c2);
        c.addAll(c2);
        System.out.println(c);
        System.out.println(c.size()); //4
    }
    @Test
    public void test3() {
        Collection c = new ArrayList();
        c.add(1);
        c.add("2");
        c.add("2");
        c.add(new Object());
        c.add(new Person("sail",19));
        //clear()方法
//        c.clear();
        System.out.println(c.size());
//        remove()
        //remove 方法会先判断删除的元素是否相等 equals方法 需要重写equals方法 默认的equals方法是 Object类的
        //只删除一个
        c.remove("2");
        System.out.println(c);
        Collection c2 = new ArrayList();
        c2.add("2");
        c2.add(new Person("sail",19));
        c.removeAll(c2);
        System.out.println(c);


    }
    @Test
    public void test4() {
        Collection c = new ArrayList();
        c.add(1);
        c.add("2");
        c.add(2);
        c.add(new Object());
        c.add(new Person("sail",19));
//      contains 方法 比较的也是equals方法  而不是 ==  “地址值”
        System.out.println(c.contains("2")); //true
        System.out.println(c.contains(2)); //true
        System.out.println(c.contains(new Person("sail",19))); //

    }
    @Test
    public void test5() {
//        hashCode()
//        ToArray()
    }
    @Test
    public void test6() {
//        集合=>数组 调用 ToArray()
        Collection c = new ArrayList();
        c.add(1);
        c.add("2");
        c.add(2);
        Object [] o = c.toArray();
        System.out.println(o);



//        数组=>集合 Array.asList()
        Collection c2 = Arrays.asList(o);
        System.out.println(c2);

    }
}
class Person {
    String name;
    int age;
     public Person(String name, int age) {
         this.name = name;
         this.age = age;
     }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

//    @Override
//    public int hashCode() {
//        return Objects.hash(name, age);
//    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';

    }
}